package org.example.uuadb.config;

import org.example.uuadb.entity.UserInfo;
import org.example.uuadb.service.UserService;
import org.springframework.security.core.userdetails.*;

import javax.annotation.Resource;
import java.util.ArrayList;

/**
 * @author : yurui
 * @date : 2024/6/2
 * @TIME : 16:41
 * @project : AuthDemo
 */

public class DBUserDetailsService implements UserDetailsService {


    @Resource
    private UserService userService;
    /**
     * 从数据库中获取用户信息
     * @param username 用户名
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        UserInfo userInfo = userService.selectUserByName(username);
        if (userInfo == null){
            throw new UsernameNotFoundException(username);
        }else {
            return new User(userInfo.getUsername(),userInfo.getPassword(),
                    userInfo.getEnabled() == 1,
                    true,
                    true,
                    true,
                    new ArrayList<>());
        }
    }
}
